package com.hsh.controller;


import com.hsh.annotations.annotation.OperationLog;
import com.hsh.entity.DbUser;
import com.hsh.persist.BaseController;
import com.hsh.service.IDbUserService;
import com.hsh.tools.JWTUtils;
import com.hsh.utils.vo.Response;
import com.hsh.vo.dto.ActiveUser;
import com.hsh.vo.param.UserLoginParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 黄思海
 * @since 2021-06-26
 */
@Api(value = "UserController", tags = "用户控制器")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IDbUserService userService;


    /**
     * 用户登录
     */
    @OperationLog(operation = "用户登录", exceptionMessage = "用户登录异常")
    @ApiOperation(value = "用户登录", notes = "用户登录", response = ActiveUser.class)
    @PostMapping("/login")
    public Response login(@RequestBody @Validated UserLoginParam param) {
        String token = userService.checkLogin(param);
        ActiveUser activeUser = JWTUtils.getActiveUser(token);
        activeUser.setToken(token);
        return Response.success(activeUser);
    }


    @ApiOperation(value = "auth", notes = "auth", response = DbUser.class)
    @GetMapping("/auth")
    @RequiresPermissions({"record-apply-for:view"})
    public Response auth() {
        List<DbUser> list = userService.list();
        return Response.success(list);
    }


}
